// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import API_OPERATE from './API_OPERATE'
import API_LOAD from './API_LOAD'

// ============================== 导出异步调用 ============================== //

export default {
  // —————————————————————————————— 提交 —————————————————————————————— //

  async remoteSubmit({ state: { one } }) {
    const msg = await API_OPERATE.submit(one, one.TIP)
    return msg
  },

  // —————————————————————————————— 下载 —————————————————————————————— //

  async remoteDownload({ state: { downParam } }) {
    const msg = await API_LOAD.download(downParam)
    return msg
  },

  // —————————————————————————————— 上传 —————————————————————————————— //

  async remoteUpload({ state: { upParam, upProgress } }) {
    const bytes = await CORE.fileBytes(upParam.file)
    await API_LOAD.upload({ content: bytes })
    upProgress.file = upParam.file
  },

  // —————————————————————————————— 上传进度 —————————————————————————————— //

  async remoteUpProgress({ state: { upParam } }) {
    const resp = await API_LOAD.upProgress(upParam)
    return {
      finish: !resp.success,
      tip: resp.title,
      num: resp.total,
      total: resp.obj,
      percent: resp.progress,
      success: resp.progress === 100,
    }
  },
}
